/*
 * NIST Healthcare Test Framework
 * MockUpClient.java Jul 23, 2010
 *
 * This code was produced by the National Institute of Standards and
 * Technology (NIST). See the "nist.disclaimer" file given in the distribution
 * for information on the use and redistribution of this software.
 */
package gov.nist.healthcare.testframework.junit;

import gov.nist.healthcare.core.MalformedMessageException;
import gov.nist.healthcare.core.message.HL7Message;
import gov.nist.healthcare.core.message.v2.er7.Er7Message;
import gov.nist.healthcare.core.message.v2.xml.XmlMessage;
import gov.nist.healthcare.core.message.v3.HL7V3Message;
import gov.nist.healthcare.testframework.TestFramework;
import gov.nist.healthcare.testframework.communication.MllpUtils;
import gov.nist.healthcare.testframework.communication.SoapUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;

/**
 * This is a mock up client for sending message for testing.
 * 
 * @author Sydney Henrard (NIST)
 */
public class MockUpClient {

    /**
     * Send a message using MLLP protocol
     * 
     * @param message
     * @param port
     * @param secure
     * @return the message sent by the test framework
     * @throws IOException
     * @throws UnknownHostException
     * @throws MalformedMessageException
     */
    public HL7Message sendMessageMLLP(HL7Message message, int port,
            boolean secure) throws UnknownHostException, IOException,
            MalformedMessageException {
        Socket client = null;
        if (secure) {
            SocketFactory sf = TestFramework.getInstance().getSSLContext().getSocketFactory();
            client = sf.createSocket("localhost", port);
        } else {
            client = new Socket("localhost", port);
        }
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        out.print(MllpUtils.wrapMessage(message));
        out.flush();
        String msg = MllpUtils.extractMessage(client.getInputStream());
        out.close();
        HL7Message receivedMessage = null;
        if (message instanceof Er7Message) {
            receivedMessage = new Er7Message(msg);
        } else if (message instanceof XmlMessage) {
            receivedMessage = new XmlMessage(msg);
        } else if (message instanceof HL7V3Message) {
            receivedMessage = new HL7V3Message(msg);
        }
        return receivedMessage;
    }

    /**
     * Send a message using SOAP protocol
     * 
     * @param message
     * @param port
     * @param secure
     * @return the message sent by the test framework
     * @throws IOException
     * @throws UnknownHostException
     * @throws MalformedMessageException
     */
    public HL7Message sendMessageSOAP(HL7Message message, int port,
            boolean secure) throws UnknownHostException, IOException,
            MalformedMessageException {
        Socket client = null;
        if (secure) {
            SocketFactory sf = TestFramework.getInstance().getSSLContext().getSocketFactory();
            client = sf.createSocket("localhost", port);
        } else {
            client = new Socket("localhost", port);
        }
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        out.print(SoapUtils.wrapMessageInHttpRequest(message, "/"));
        out.flush();
        String msg = SoapUtils.unWrapMessageFromHttpResponse(client);
        out.close();
        HL7Message receivedMessage = null;
        if (message instanceof Er7Message) {
            receivedMessage = new Er7Message(msg);
        } else if (message instanceof XmlMessage) {
            receivedMessage = new XmlMessage(msg);
        } else if (message instanceof HL7V3Message) {
            receivedMessage = new HL7V3Message(msg);
        }
        return receivedMessage;
    }
}
